// pages/shoppingcard/shoppingcard.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 购物车商品列表
    cartList: [{
        id: 1,
        img: '../../image/apple.png',
        name: '2个招牌-猕宗绿奇异果大单果100g起',
        spec: '单果100g起',
        price: 9.90,
        count: 1,
        checked: true
      },
      {
        id: 2,
        img: '../../image/apple.png',
        name: '【剥肉】B级红心柚子肉果切250g/盒',
        spec: '250g/盒',
        price: 9.90,
        count: 1,
        checked: true
      },
      {
        id: 3,
        img: '../../image/apple.png',
        name: '【剥肉】B级红心柚子肉果切250g/盒',
        spec: '250g/盒',
        price: 9.90,
        count: 1,
        checked: true
      },
      {
        id: 4,
        img: '../../image/apple.png',
        name: '【剥肉】B级红心柚子肉果切250g/盒',
        spec: '250g/盒',
        price: 9.90,
        count: 1,
        checked: true
      },
      {
        id: 5,
        img: '../../image/apple.png',
        name: '【剥肉】B级红心柚子肉果切250g/盒',
        spec: '250g/盒',
        price: 9.90,
        count: 1,
        checked: true
      },
      {
        id: 6,
        img: '../../image/apple.png',
        name: '【剥肉】B级红心柚子肉果切250g/盒',
        spec: '250g/盒',
        price: 9.90,
        count: 1,
        checked: true
      },
      {
        id: 7,
        img: '../../image/apple.png',
        name: '【剥肉】B级红心柚子肉果切250g/盒',
        spec: '250g/盒',
        price: 9.90,
        count: 1,
        checked: true
      }
    ],
    allChecked: true, // 全选状态
    totalCount: 2, // 商品总数
    checkedCount: 2, // 选中商品数量
    totalPrice: 19.80, // 选中商品总价
    discount: 0, // 优惠金额
    freightThreshold: 49, // 免运费门槛
    lackAmount: 29.20 // 还差金额免运费
  },
  // 切换商品勾选状态
  toggleCheck(e) {
    const goodsId = e.currentTarget.dataset.id;
    const newCartList = this.data.cartList.map(item => {
      if (item.id === goodsId) {
        return {
          ...item,
          checked: !item.checked
        };
      }
      return item;
    });
    this.setData({
      cartList: newCartList
    }, () => {
      this.calcCartData(); // 重新计算购物车数据
    });
  },

  // 全选/取消全选
  toggleAllCheck() {
    const newAllChecked = !this.data.allChecked;
    const newCartList = this.data.cartList.map(item => ({
      ...item,
      checked: newAllChecked
    }));
    this.setData({
      allChecked: newAllChecked,
      cartList: newCartList
    }, () => {
      this.calcCartData();
    });
  },

  // 减少商品数量
  minusCount(e) {
    const goodsId = e.currentTarget.dataset.id;
    const newCartList = this.data.cartList.map(item => {
      if (item.id === goodsId && item.count > 1) {
        return {
          ...item,
          count: item.count - 1
        };
      }
      return item;
    });
    this.setData({
      cartList: newCartList
    }, () => {
      this.calcCartData();
    });
  },

  // 增加商品数量
  addCount(e) {
    const goodsId = e.currentTarget.dataset.id;
    const newCartList = this.data.cartList.map(item => {
      if (item.id === goodsId) {
        return {
          ...item,
          count: item.count + 1
        };
      }
      return item;
    });
    this.setData({
      cartList: newCartList
    }, () => {
      this.calcCartData();
    });
  },

  // 计算购物车数据（总数、选中数、总价、运费差）
  calcCartData() {
    const {
      cartList,
      freightThreshold
    } = this.data;

    // 商品总数
    const totalCount = cartList.reduce((sum, item) => sum + item.count, 0);

    // 选中商品统计
    const checkedGoods = cartList.filter(item => item.checked);
    const checkedCount = checkedGoods.reduce((sum, item) => sum + item.count, 0);
    const totalPrice = checkedGoods.reduce((sum, item) => sum + (item.price * item.count), 0).toFixed(2);

    // 全选状态（所有商品都选中时为true）
    const allChecked = cartList.every(item => item.checked) && cartList.length > 0;

    // 运费差额
    const lackAmount = Math.max(0, freightThreshold - totalPrice).toFixed(2);

    this.setData({
      totalCount,
      checkedCount,
      totalPrice,
      allChecked,
      lackAmount
    });
  },

  // 去结算
  goSettle() {
    if (this.data.checkedCount === 0) {
      wx.showToast({
        title: '请选择商品',
        icon: 'none'
      });
      return;
    }
    // 跳转到结算页面（实际项目中可传递选中的商品数据）
    wx.navigateTo({
      url: '/pages/settle/settle'
    });
  },

  // 去凑单
  goAdd() {
    wx.navigateTo({
      url: '/pages/index/index' // 跳转到首页或商品列表页
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.calcCartData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})